package com.citcc.web.controller.business;

import com.citcc.business.bean.CdzGun;
import com.citcc.business.bean.CdzManufacturer;
import com.citcc.business.bean.CdzStake;
import com.citcc.business.bean.CdzStation;
import com.citcc.business.service.CdzGunService;
import com.citcc.business.service.CdzManufacturerService;
import com.citcc.business.service.CdzStakeService;
import com.citcc.business.service.CdzStationService;
import com.citcc.common.annotation.Log;
import com.citcc.common.base.AjaxResult;
import com.citcc.common.enums.BusinessType;
import com.citcc.common.page.TableDataInfo;
import com.citcc.framework.util.ShiroUtils;
import com.citcc.framework.web.base.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cdzStake")
public class CdzStakeController  extends BaseController {

    private String prefix = "business/cdzStake";

    @Autowired
    private CdzStakeService cdzStakeService;
    @Autowired
    private CdzManufacturerService manufacturerService;
    @Autowired
    private CdzStationService cdzStationService;
    @Autowired
    private CdzGunService cdzGunService;

    @RequiresPermissions("business:cdzStake:view")
    @GetMapping("/main")
    public String vehicle(ModelMap map){
        CdzStation station = new CdzStation();
        station.setOrgId(getSysUser().getDeptId());
        map.put("station",cdzStationService.selectCdzStationAll(station));
        return prefix + "/main";
    }

    @RequiresPermissions("business:cdzStake:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(CdzStake stake){
        startPage();
        if (stake.getOrgId()==null||"".equals(stake.getOrgId())) {
            stake.setOrgId(getSysUser().getDeptId());
        }
        List<CdzStake> list=cdzStakeService.selectAllList(stake);
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String toAdd(ModelMap map)
    {
        map.put("station",cdzStationService.selectCdzStationAll(new CdzStation()));
        map.put("trademarkList",manufacturerService.selectAllList(new CdzManufacturer()));
        return prefix + "/add";
    }

    @RequiresPermissions("business:cdzStake:add")
    @Log(title = "充电桩管理", businessType = BusinessType.INSERT)
    @PostMapping("/addSave")
    @ResponseBody
    public AjaxResult addSave(CdzStake stake)
    {
        if (stake.getOrgId()==null||"".equals(stake.getOrgId())) {
            stake.setOrgId(getSysUser().getDeptId());
        }
        stake.setCreateBy(ShiroUtils.getLoginName());
        stake.setDelFlag("0");
        return toAjax(cdzStakeService.add(stake));
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id")String id, ModelMap map)
    {
        map.put("station",cdzStationService.selectCdzStationAll(new CdzStation()));
        map.put("trademarkList",manufacturerService.selectAllList(new CdzManufacturer()));
        map.put("stake",cdzStakeService.getCdzStake(id));
        return prefix + "/edit";
    }


    @RequiresPermissions("business:cdzStake:edit")
    @Log(title = "充电桩管理", businessType = BusinessType.UPDATE)
    @PostMapping("/editSave")
    @ResponseBody
    public AjaxResult editSave(CdzStake stake)
    {
        stake.setUpdateBy(ShiroUtils.getLoginName());
        stake.setUpdateTime(new Date());
        return toAjax(cdzStakeService.update(stake));
    }

    @RequiresPermissions("business:cdzStake:remove")
    @Log(title = "充电桩管理", businessType = BusinessType.UPDATE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String id)
    {
        try
        {
            CdzStake stake=cdzStakeService.getCdzStake(id);
            int count=cdzGunService.existStake(stake.getCode());
            if(count>0){
                return error("该充电桩存在充电枪不能删除");
            }else{
                Integer e =cdzStakeService.delete(id);
                return toAjax(e);
            }
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 添加充电枪
     * @param id
     * @param map
     * @return
     */
    @GetMapping("/addGun/{id}")
    public String addGun(@PathVariable("id")String id, ModelMap map)
    {
        CdzStake stake=cdzStakeService.getCdzStake(id);
        map.put("orgId",stake.getOrgId());
        map.put("stakeCode",stake.getCode());
        return prefix + "/addGun";
    }

    /**
     * 展示充电枪信息
     */
    @GetMapping("/toGunList/{stakeId}")
    public String toGunList(@PathVariable("stakeId")String stakeId, ModelMap map)
    {
        map.put("stakeId",stakeId);
        return prefix + "/gunList";
    }

}
